import type { Router } from 'vue-router'
import { useAuthStore } from '@/store/modules/auth'
import { useChanceStore } from '@/store/modules/chance'
// import { getToken } from '@/utils/auth' 


export function setupPageGuard(router: Router) {
  router.beforeEach(async (from, to, next) => {
    const authStore = useAuthStore()
    // try {
    //     //已经登录
    //   if (authStore.token!=undefined) {
    //       if (from.name === 'Login') {
    //         next( "/chat/"+useUserStore().userInfo.phonenum)
    //       } else{
    //           next()
    //       }
    //   }
    //   //未登录
    //   else {
    //     console.log('chanace',chanceState.chance)
    //     next('/chat/1002')
    //     // if (whiteList.indexOf(from.path) !== -1) {
    //     //   next()
    //     // }else {
    //     //   next({ name: 'Chat'})
    //     // }
    //   }
    // }
    //   catch (error) {
    //     if (from.path !== '/500')
    //       next({ name: '500' })
    //     else
    //       next()
    //   }

    if (!authStore.session) {
      try {
        // const data = await authStore.getSession()
        // // if (String(data.auth) === 'false' && authStore.token)
        // //   authStore.removeToken()
        next()
      }
      catch (error) {
        if (from.path !== '/500')
          next({ name: '500' })
        else
          next()
      }
    }
    else {
      next()
    }

  })
}
